package cn.chrisx.drad.dr;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import cn.chrisx.drad.DrAdSdk;
import cn.chrisx.drad.core.DrAdConfig;
import cn.chrisx.drad.core.DrAdError;
import cn.chrisx.drad.core.DrAdPlatform;
import cn.chrisx.drad.core.DrAdType;
import cn.chrisx.drad.wrap.Ad;

/**
 * @author Chris <jz_boy@163.com>
 * @version 1.0
 * @date 2018/9/21 18:46
 * @description
 * @copyright ©2018 www.jzfox.net All rights reserved.
 */
public abstract class DrAbstractAd {

    // 广告加载完成后立即展示
    private boolean autoShow;

    private Context context;

    // 是否在请求中
    private boolean request;

    // 是否已经 destroy
    private boolean destroy;

    // 广告的具体执行类
    private Ad executor;

    private DrListenerIntercept listenerIntercept = new DrListenerIntercept(null, null);

    private final List<DrAdConfig> adConfigs = new ArrayList<>();

    private final LinkedList<DrAdConfig> tempAdConfig = new LinkedList<>();

    DrAbstractAd() {
        listenerIntercept.setDrAd(this);
    }


    /**
     * 设置广告拦截器
     *
     * @param listenerIntercept
     */
    public void setListenerIntercept(@NonNull DrListenerIntercept listenerIntercept) {
        listenerIntercept.setDrAd(this);
        this.listenerIntercept = listenerIntercept;
    }

    /**
     * 加载广告
     *
     * @param context
     * @return
     */
    public boolean loadAd(@NonNull Context context) {

        if (destroy) return false;

        if (request) {
            listenerIntercept.onAdLoadFailed(DrAdError.ERROR_REQUEST);
            return false;
        }

        this.context = context;
        request = true;

        tempAdConfig.clear();
        tempAdConfig.addAll(adConfigs);

        load();

        return true;
    }

    boolean isAutoShow() {
        return autoShow;
    }

    public void onResume(Activity activity) {
        if (executor != null) executor.onResume(activity);
    }

    public void onPause(Activity activity) {
        if (executor != null) executor.onPause(activity);
    }

    /**
     * 设置广告加载完成后立即展示
     *
     * @param autoShow true 广告加载成功后立即展示； false 手动调用 {{@link #show()}}
     */
    public void setAutoShow(boolean autoShow) {
        this.autoShow = autoShow;
    }

    /**
     * 添加广告的平台的相关配置
     *
     * @param config
     */
    public void addAdConfig(@NonNull DrAdConfig config) {
        adConfigs.add(config);
    }

    public void setAdConfigs(@NonNull DrAdConfig[] configs) {
        adConfigs.addAll(Arrays.asList(configs));
    }

    public void setAdListener(DrAdListener listener) {
        this.listenerIntercept.setAdListener(listener);
    }

    public void setRewardAdListener(DrRewardAdListener listener) {
        this.listenerIntercept.setRewardAdListener(listener);
    }

    public void onDestroy(@NonNull Context context) {
        destroy = true;
        if (executor != null) executor.onDestroy(context);
        executor = null;
    }

    /**
     * 是否是没有被 destroy
     *
     * @return
     */
    boolean isAlive() {
        return !destroy;
    }

    protected abstract DrAdType adType();

    /**
     * 广告展示
     *
     * @return
     */
    public boolean show() {
        return !destroy && isValid() && executor.show();
    }

    /**
     * wrapper 中可以用调用
     */
    void load() {

        if (executor != null) {
            executor.onDestroy(context);
        }

        if (tempAdConfig.isEmpty()) {
            listenerIntercept.onAdLoadFailed(DrAdError.ERROR_NOT_CONFIG);
            return;
        }

        DrAdConfig config;

        while (!destroy) {

            config = tempAdConfig.poll();

            listenerIntercept.setCurrentConfig(null);

            if (config == null) {

                if (tempAdConfig.isEmpty()) {
                    listenerIntercept.onAdLoadFailed(DrAdError.ERROR_NO_FILL);
                    break;
                } else {
                    continue;
                }
            }

            // 仅没测试时启用
            if (config.isOnlyDebug()) {
                if (!DrAdSdk.isDebug()) {
                    listenerIntercept.onAdLoadingError(config, DrAdError.ERROR_DEBUG, null);
                    continue;
                }
            }

            // ---- 请求广告开始 ----
            DrAdPlatform dp = DrAdSdk.findPlatform(config.platform().name());

            if (dp == null) {
                listenerIntercept.onAdLoadingError(config, DrAdError.ERROR_NOT_CONFIG, null);
                continue;
            }

            Ad ad = dp.executor(adType());

            if (ad == null) {
                listenerIntercept.onAdLoadingError(config, DrAdError.ERROR_NOT_SUPPORT, null);
                continue;
            }

            executor = ad;
            listenerIntercept.setCurrentConfig(config);

            // 设置相关参数
            ad.setAdListener(listenerIntercept);
            ad.setAdConfig(config);
            ad.loadAd(context);
            break;
        }

    }


    /**
     * 广告是否有效期内
     *
     * @return
     */
    public boolean isValid() {
        return executor != null && executor.isValid();
    }


    // 调用广告成功的平台记录
    private DrAdPlatform successPlatform;

    /**
     * 设置当前平台信息
     *
     * @param platform
     */
    void setPlatform(DrAdPlatform platform) {
        successPlatform = platform;
    }

    public DrAdPlatform getPlatform() {
        return successPlatform;
    }

    protected Ad executor() {
        return executor;
    }

}
